var a = require("../../../utils/httpPost.js"), t = require("../../../component/microPage/common/data/index.js"), 
app = getApp();

Page({
  data: {
    userId: 0,
    menuId: 50,
    title: "",
    isLoading: true,
    loading:false,
    pageIndex:1,
    pageSize:20,
    sections:[],
    current: 0,
    banners:[],
    isiphonex: app.globalData.isiphonex,
    isxiaomi: app.globalData.isxiaomi,
  },
  onLoad: function (options) {
    console.log('onLoad options',options)
    
    if(options.q){
      var q = decodeURIComponent(options.q) // 获取到二维码原始链接内容
      const scancode_time = parseInt(options.scancode_time) // 获取用户扫码时间 UNIX 时间戳
      
      q = "?" + q.split("?")[1];
      this.data.query = q;
      console.log('q',q)
      options = this.getQueryVariable(q);
      console.log('options',options)
      var id = parseInt(options.id) 
      this.data.menuId = id;
    }else{
      if(options!=undefined &&options.id){
        this.data.menuId = parseInt(options.id)
      }
    }
    this.data.userId = app.getUserId();
    this.loadData();
  },
  onShow: function (a) {
    //自定义的tabbar
  },
  getQueryVariable(url){
    //返回当前 URL 的查询部分（问号 ? 之后的部分）。
    var urlParameters = url;// location.search;
    //声明并初始化接收请求参数的对象
    var requestParameters = new Object();
    var parameterArray = [];
    //如果该求青中有请求的参数，则获取请求的参数，否则打印提示此请求没有请求的参数
    if (urlParameters.indexOf('?') != -1)
    {
        //获取请求参数的字符串
        var parameters = decodeURI(urlParameters.substr(1));
        //将请求的参数以&分割中字符串数组
        parameterArray = parameters.split('&');
        //循环遍历，将请求的参数封装到请求参数的对象之中
        for (var i = 0; i < parameterArray.length; i++) {
            requestParameters[parameterArray[i].split('=')[0]] = (parameterArray[i].split('=')[1]);
        }
        console.info('theRequest is =====',requestParameters);
    } else
    {
        console.info('There is no request parameters');
    }
    return requestParameters;
  },
  /**
   * 获取页面数据的 http请求
   */
  loadData: function () {
    var that = this
    a.httpPost('/titleMenu/list', {
      title_menu_id: that.data.menuId,
      pageIndex: that.data.pageIndex,
      pageSize:  that.data.pageSize,
    }, function (res) {
      console.log('活动页页数据', res)
      if (res.data.code == 0) {
        wx.setNavigationBarTitle({
          title: res.data.data.titleMenu.titleName,
        })
        if(res.data.totalcount > 0){
          that.data.pageIndex = that.data.pageIndex + 1;
          var sections = res.data.data.sections;
          if(sections.length > 0){
            for (var index in sections) {
              that.data.sections.splice(that.data.sections.length, 0, sections[index]);
            }
            that.setData({
              hasData: true,
              sections: that.data.sections
            })
          }
        }

        that.setData({
          banners:res.data.data.banners,
          isLoading: false, loading: true, netWork: true, dataIsLoad:true,
        });
      } else {
        that.setData({ isLoading: false, loading: true, netWork: true, dataIsLoad: false })
      }
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    }, function () {
      that.setData({ isLoading: false, loading: true, netWork: true, dataIsLoad: false})
    })
  },
  onPullDownRefresh: function () {
    this.data.pageIndex = 1;
    this.setData({
      isLoading:true,
      sections:[],
    })
    this.loadData();
  },
  /**
   * 页面上拉触底事件的处理函数    加载更多
   */
  onReachBottom: function () {
    this.loadData();
  },
});